package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderList;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderlistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orderlist")
public class OrderListController {


    @Reference
    private OrderlistService orderlistService;

    //查询所有数据
    @RequestMapping("/findPage")
    public Result findpage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult page = orderlistService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, page);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    //新增预约信息
    @RequestMapping("/add")
    public Result add(@RequestBody OrderList orderList,Integer[] setmealIds) {
        try {
            orderlistService.add(orderList,setmealIds);
            return new Result(true, MessageConstant.ADD_reserve_success);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_reserve_fail);
        }
    }
    //确认预约信息发送短信
    @RequestMapping("/affirmOrder")
    public Result affirmOrder(Integer id){
        try {
            orderlistService.sendMessage(id);
            orderlistService.updateOrderStatus(id);
            return new Result(true,MessageConstant.Confirm_the_reservation);
        } catch (Exception e) {
            return new Result(false,MessageConstant.Failed_to_confirm);
        }
    }
    //取消预约信息，删除表中数据
    @RequestMapping("/cancelOrder")
    public Result cancelOrder(Integer id){
        try {
            orderlistService.deletOrder(id);
            return new Result(true, MessageConstant.DELET_ORDER_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.DELET_ORDER_FAIL);
        }
    }

    //批量取消预约信息
    @RequestMapping("/DeletOrder")
    public Result DeletOrder(@RequestBody OrderList[] orderList){
        try {
            orderlistService.BatchDeletOrder(orderList);
            return new Result(true, MessageConstant.Bulk_cancellations_SUCCESS);
        } catch (Exception e) {
            return new Result(false,MessageConstant.Bulk_cancellations_FAIL);
        }
    }

    //获取所有套餐
    @RequestMapping("/findsetmeal")
    public Result findsetmeal(){
        List<Setmeal> setMeal= null;
        try {
            setMeal = orderlistService.findsetmeal();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(true,"",setMeal);
    }
}
